#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

void sys_err(const char *str){
	perror(str);
	exit(1);
}

void *tfn(void *arg){
	while(1){
		printf("thread: pid = %d, tid=%ld\n",getpid(),pthread_self());
		//sleep(1);
		pthread_testcancel();//自己添加取消点，结束线程要进如内核
	}
	return NULL;
}



int main(int argc, char *argv[]){
	pthread_t tid;
	int ret = pthread_create(&tid,NULL,tfn,NULL);
	if (ret != 0){
		sys_err("pthread_create error");
	}
	printf("main:pid = %d, tid = %lu\n",getpid(),pthread_self());
	sleep(5);
	ret = pthread_cancel(tid);//终止线程
	if(ret !=0){
		fprintf(stderr,"thread_cancel: %s\n",strerror(ret));
		exit(1);
	}	
	pthread_exit((void*)0);
	
}


